﻿package com.andf.basic;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.andf.common.exception.MIException;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public abstract class BaseAndfAction<T> extends ActionSupport implements
		IAndfAction, ModelDriven<T>, Preparable {

	private static final long serialVersionUID = -3133313516843731302L;

	/*   * 0:正常 MI开头的编码为用户关系异常 1:运行时异常 -1:其他异常 * */
	private String retcode = "0";

	private String retmsg = null;

	// 实体对象
	protected T entity;

	private String ids;
	
	public String list() throws RuntimeException {
		String ret = null;
		try {
			ret = dolist();
		} catch (MIException mi) {
			setRetcode(mi.getCode());
			setRetmsg(mi.getMessage());
		} catch (RuntimeException re) {
			setRetcode("1");
			setRetmsg(re.getMessage());
			re.printStackTrace();
		} catch (Exception e) {
			setRetcode("-1");
			setRetmsg(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}
	
	public String qryOne() throws RuntimeException {
		String ret = null;
		try {
			ret = doqryOne();
		} catch (MIException mi) {
			setRetcode(mi.getCode());
			setRetmsg(mi.getMessage());
		} catch (RuntimeException re) {
			setRetcode("1");
			setRetmsg(re.getMessage());
			re.printStackTrace();
		} catch (Exception e) {
			setRetcode("-1");
			setRetmsg(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}
	
	public String insert() throws RuntimeException {
		String ret = null;
		try {
			ret = doinsert();
		} catch (MIException mi) {
			setRetcode(mi.getCode());
			setRetmsg(mi.getMessage());
		} catch (RuntimeException re) {
			setRetcode("1");
			setRetmsg(re.getMessage());
			re.printStackTrace();
		} catch (Exception e) {
			setRetcode("-1");
			setRetmsg(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}
	
	public String update() throws RuntimeException {
		String ret = null;
		try {
			ret = doupdate();
		} catch (MIException mi) {
			setRetcode(mi.getCode());
			setRetmsg(mi.getMessage());
		} catch (RuntimeException re) {
			setRetcode("1");
			setRetmsg(re.getMessage());
			re.printStackTrace();
		} catch (Exception e) {
			setRetcode("-1");
			setRetmsg(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}
	
	public String delete() throws RuntimeException {
		String ret = null;
		try {
			ret = dodelete();
		} catch (MIException mi) {
			setRetcode(mi.getCode());
			setRetmsg(mi.getMessage());
		} catch (RuntimeException re) {
			setRetcode("1");
			setRetmsg(re.getMessage());
			re.printStackTrace();
		} catch (Exception e) {
			setRetcode("-1");
			setRetmsg(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}
	
	/*@Override
	public String execute() throws RuntimeException {
		String ret = null;
		try {
			ret = doexecute();
		} catch (MIException mi) {
			setRetcode(mi.getCode());
			setRetmsg(mi.getMessage());
		} catch (RuntimeException re) {
			setRetcode("1");
			setRetmsg(re.getMessage());
			re.printStackTrace();
		} catch (Exception e) {
			setRetcode("-1");
			setRetmsg(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}*/

	public String getRetcode() {
		return retcode;
	}

	public void setRetcode(String retcode) {
		this.retcode = retcode;
	}

	public String getRetmsg() {
		return retmsg;
	}

	public void setRetmsg(String retmsg) {
		this.retmsg = retmsg;
	}

	/**
	 * 
	 * @Title: dolist 
	 * @author lilubing
	 * @Description: TODO(与struts.xml调用名字list调用方法,子类重写即可;<br>统一异常;)
	 * @param @return
	 * @param @throws RuntimeException
	 * @return String
	 * @throws
	 * @date 2013-4-25 下午03:07:48
	 */
	abstract protected String dolist() throws RuntimeException, Exception;
	/**
	 * 
	 * @Title: doqryOne 
	 * @author lilubing
	 * @Description: TODO(与struts.xml调用名字qryOne调用方法,子类重写即可;<br>统一异常;)
	 * @param @return
	 * @param @throws RuntimeException
	 * @return String
	 * @throws
	 * @date 2013-4-25 下午03:07:48
	 */
	abstract protected String doqryOne() throws RuntimeException, Exception;
	/**
	 * 
	 * @Title: doinsert 
	 * @author lilubing
	 * @Description: TODO(与struts.xml调用名字insert调用方法,子类重写即可;<br>统一异常;)
	 * @param @return
	 * @param @throws RuntimeException
	 * @return String
	 * @throws
	 * @date 2013-4-25 下午03:07:48
	 */
	abstract protected String doinsert() throws RuntimeException, Exception;
	/**
	 * 
	 * @Title: doupdate 
	 * @author lilubing
	 * @Description: TODO(与struts.xml调用名字update调用方法,子类重写即可;<br>统一异常;)
	 * @param @return
	 * @param @throws RuntimeException
	 * @return String
	 * @throws
	 * @date 2013-4-25 下午03:07:48
	 */
	abstract protected String doupdate() throws RuntimeException, Exception;
	/**
	 * 
	 * @Title: dodelete 
	 * @author lilubing
	 * @Description: TODO(与struts.xml调用名字delete调用方法,子类重写即可;<br>统一异常;)
	 * @param @return
	 * @param @throws RuntimeException
	 * @return String
	 * @throws
	 * @date 2013-4-25 下午03:07:48
	 */
	abstract protected String dodelete() throws RuntimeException, Exception;

	@Override
	public T getModel() {
		return entity;
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}

	/**
	 * 根据泛型类型实例化对象 添加
	 * 
	 * @return 泛型类型的对象
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public T instanceAnnotationObject() throws Exception {
		Class<?> cl = this.getClass();
		Type[] types = ((ParameterizedType) cl.getGenericSuperclass())
				.getActualTypeArguments();
		try {
			return ((Class<T>) types[0]).newInstance();
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void prepare() throws Exception {
		this.entity = this.instanceAnnotationObject();
	}

	/**
	 * 获取request
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取response
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获取session
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}
	
	protected UserInfo getUserInfo(){
		UserInfo info = new UserInfo();
		info.setUserId("1");
		info.setUsername("佚名");
		return info ;
	}
	
	public String[] getIdsArrayResult() {
		String[] idArray = null;
		if(StringUtils.isNotEmpty(ids)) {
			idArray = ids.split(",");
		}
		return idArray;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
}
